package kr.hkit.android_caloriecheck.fragment;

import java.util.ArrayList;
import java.util.Calendar;

import kr.hkit.android_caloriecheck.DBHelper;
import kr.hkit.android_caloriecheck.R;
import kr.hkit.android_caloriecheck.adapter.MyListAdapter;
import kr.hkit.android_caloriecheck.dao.DAO;
import kr.hkit.android_caloriecheck.dto.MyItem;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

public class FoodFragment extends Fragment {
	DBHelper mHelper;
	DAO dao;
	ListView list;
	EditText food;
	Button search;
	String eatName;
	int eatKcal;
	Calendar calendar;
	int year;
	int month;
	int day;

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		final View root = inflater.inflate(R.layout.food_add, container, false);

		calendar = Calendar.getInstance();
		year = calendar.get(Calendar.YEAR);
		month = calendar.get(Calendar.MONTH);
		day = calendar.get(Calendar.DATE);

		mHelper = new DBHelper(getActivity());
		dao = DAO.open(getActivity());
		list = (ListView) root.findViewById(R.id.list_food);
		food = (EditText) root.findViewById(R.id.edit_food_add);
		search = (Button) root.findViewById(R.id.btn_food_search);
		search.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				ArrayList<MyItem> arList;
				String foodname = food.getText().toString().trim();

				if (foodname.equals("")) {
					arList = dao.foodSelectAll();
				} else {
					arList = dao.foodSelectByName(foodname);
				}

				if (arList.size() == 0) {
					final LinearLayout linear = (LinearLayout) View.inflate(
							getActivity(), R.layout.food_add02, null);

					new AlertDialog.Builder(getActivity())
							.setTitle("검색결과없음")
							.setView(linear)
							.setPositiveButton("추가",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog,
												int which) {
											EditText foodname = (EditText) linear
													.findViewById(R.id.edit_foodname);
											EditText foodcal = (EditText) linear
													.findViewById(R.id.edit_foodcal);

											String name = foodname.getText()
													.toString().trim();
											String cal = foodcal.getText()
													.toString().trim();

											try {
												validCheck(name, cal);
												dao.foodInsert(name,
														Integer.parseInt(cal));
												Toast.makeText(
														getActivity(),
														name + " " + cal
																+ " 추가",
														Toast.LENGTH_SHORT)
														.show();

											} catch (Exception e) {
												Toast.makeText(getActivity(),
														e.getMessage(),
														Toast.LENGTH_SHORT)
														.show();
											}
										}
									}).setNegativeButton("취소", null).show();

				} else {
					MyListAdapter adapter = new MyListAdapter(
							getActivity(), arList, R.layout.list, "food");
					ListView myList = (ListView) root
							.findViewById(R.id.list_food);
					myList.setAdapter(adapter);					
				}
			}
		});

		return root;
	}

	public void onPause() {
		super.onPause();
		food.setText("");
	}

	public void validCheck(String name, String cal) throws Exception {
		if (name.equals("")) {
			throw new Exception("음식명을 입력해주세요.");
		}
		if (cal.equals("")) {
			throw new Exception("칼로리를 입력해주세요.");
		}

		ArrayList<MyItem> arList = dao.foodSelectByName2(name);
		if (arList.size() != 0) {
			throw new Exception("이미 등록된 정보입니다.");
		}
	}
}
